/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.TX;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import Helper.*;
import javax.ejb.Timeout;

import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;




/**
 *
 * @author Randall Chia
 */
@Singleton
public class SchedulerSB implements SchedulerSBRemote {
    
@EJB
private StakeholderBillingSBRemote sbr;

    
    
   private boolean isBillingEnabled = false;
   
   public void enableBilling(){
       System.out.println("SchedulerBean: billing cycle enabled. Billing process will execute at the scheduled time");
       isBillingEnabled = true;
   }

   public void disableBilling(){
       System.out.println("SchedulerBean: billing cycle disabled");
       isBillingEnabled = false;
   }
    
   
   
   //To bill on the 3rd of every month: (dayOfMonth="3", persistent=false)
   @Schedule(second="*/30", minute="*",hour="*", persistent=false)
    public void doWork(){
       if(isBillingEnabled){
            
            Calendar cal = Calendar.getInstance();        
            Date currentDate = cal.getTime();
            
            cal.add(Calendar.MONTH, -1); //the previous billing date is on the previous month
            Date previousDate = cal.getTime();

            System.out.println("Billing task executed on date: " + new Date());
            sbr.billStakeholders(previousDate, currentDate);
            System.out.println("Billing task COMPLETE \n\n\n");
            
       }       
    }
   
   
    
}
